﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IBatisNet.DataMapper;
using StateGrid.OperationLog;
using StateGrid;
using System.Xml;
using StateGrid.GlobalVariables;

namespace StateGrid.ZGCG
{
    /// <summary>
    ///ZGCG_DAO 的摘要说明
    /// </summary>
    public class ZGCG_DAO
    {
        private ISqlMapper sqlMapper = Mapper.Instance();
        
        /// <summary>
        /// 日志操作对象
        /// </summary>
        private OperationLog_DAO operationLogDAO = new OperationLog_DAO();

        /// <summary>
        /// 获取所有中国成果分类
        /// </summary>
        public IList<ZGCG_FL> GetAllZGCG_FL()
        {
            return sqlMapper.QueryForList<ZGCG_FL>("SelectAllZGCG_FL", null);
        }

        /// <summary>
        /// 获取所有中国成果篇名
        /// </summary>
        public IList<ZGCG_PM> GetAllZGCG_PM()
        {
            return sqlMapper.QueryForList<ZGCG_PM>("SelectAllZGCG_PM", null);
        }

        /// <summary>
        /// 按分类编码查找分类信息
        /// </summary>
        /// <param name="flbm"></param>
        /// <returns></returns>
        public ZGCG_FL GetZGCG_FLByFlbm(String flbm)
        {
            return sqlMapper.QueryForObject<ZGCG_FL>("SelectZGCG_FLByFlbm", flbm);
        }

        /// <summary>
        /// 按专业分类查找分类信息
        /// </summary>
        /// <param name="zyfl"></param>
        /// <returns></returns>
        public ZGCG_FL GetZGCG_FLByZyfl(String zyfl)
        {
            return sqlMapper.QueryForObject<ZGCG_FL>("SelectZGCG_FLByZyfl", zyfl);
        }

        /// <summary>
        /// 按照分类编码名称查找分类信息
        /// </summary>
        /// <param name="flmc">分类名称</param>
        /// <returns></returns>
        public ZGCG_FL GetZGCG_FLByFlmc(String flmc)
        {
            return sqlMapper.QueryForObject<ZGCG_FL>("SelectZGCG_FLByFlmc", flmc);
        }

        /// <summary>
        /// 获取指定专业分类、年份的篇名数量
        /// </summary>
        /// <param name="zyfl">专业分类</param>
        /// <param name="nf">年份</param>
        public int GetCountSomeZGCG_PM(String zyfl, String nf)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();

            String cgbh = "%";
            String flbm = "%";
            //拼凑成果编码,分类编码
            if (null != zyfl && !"".Equals(zyfl))
            {
                for (int i = 0; i < zyfl.Length; i++)
                {
                    if (zyfl[i] != '.')
                    {
                        flbm = flbm + zyfl[i] + '0';
                    }
                }
                flbm = flbm + "%";
            }

            cgbh = cgbh + nf + "%";

            para.Add("cgbh", cgbh);
            para.Add("flbm", flbm);
            return sqlMapper.QueryForObject<int>("CountSomeZGCG_PM", para);
        }

        /// <summary>
        /// 获取指定专业分类、年份的篇名数量
        /// </summary>
        /// <param name="zyfl">专业分类</param>
        /// <param name="nf">年份</param>
        /// <returns></returns>
        public IList<ZGCG_PM> GetSomeZGCG_PM(String zyfl, String nf, int start, int end)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();
            String cgbh = "%";
            String flbm = "%";
            //拼凑成果编码,分类编码
            if (null != zyfl && !"".Equals(zyfl))
            {
                for (int i = 0; i < zyfl.Length; i++)
                {
                    if (zyfl[i] != '.')
                    {
                        flbm = flbm + zyfl[i] + '0';
                    }
                }
                flbm = flbm + "%";
            }

            cgbh = cgbh + nf + "%";

            para.Add("cgbh", cgbh);
            para.Add("flbm", flbm);
            para.Add("start", start);
            para.Add("end", end);

            return sqlMapper.QueryForList<ZGCG_PM>("SelectSomeZGCG_PM", para);
        }

        /// <summary>
        /// 获取指定成果编号的篇名信息
        /// </summary>
        /// <param name="cgbh">成果编号</param>
        /// <returns></returns>
        public ZGCG_PM GetZGCG_PMByCgbh(String cgbh)
        {
            return sqlMapper.QueryForObject<ZGCG_PM>("SelectZGCG_PMByCgbh", cgbh);
        }


        /// <summary>
        /// 获取指定成果名称和发布年份的篇名信息
        /// </summary>
        /// <param name="cgmc">成果名称</param>
        /// <param name="fbnf">发布年份</param>
        /// <returns></returns>
        public ZGCG_PM GetZGCG_PMByCgmcAndFbnf(String cgmc, int fbnf)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();

            para.Add("cgmc", cgmc);
            para.Add("fbnf", fbnf);

            return sqlMapper.QueryForObject<ZGCG_PM>("SelectZGCG_PMByCgmcAndFbnf", para);
        }

        /// <summary>
        /// 获取指定ID编号的篇名信息
        /// </summary>
        /// <param name="id">ID编号</param>
        /// <returns></returns>
        public ZGCG_PM GetZGCG_PMByID(int id)
        {
            return sqlMapper.QueryForObject<ZGCG_PM>("SelectZGCG_PMByID", id);
        }

        /// <summary>
        /// 插入一条分类信息
        /// </summary>
        /// <param name="zgcg_fl">分类信息</param>
        /// <returns></returns>
        public Boolean InsertZGCG_FL(ZGCG_FL zgcg_fl)
        {
            Boolean flag = true;
            try
            {
                sqlMapper.Insert("InsertZGCG_FL", zgcg_fl);
            }
            catch (Exception)
            {
                flag = false;
            }

            return flag;
        }

        /// <summary>
        /// 插入一条篇名信息
        /// </summary>
        /// <param name="zgcg_pm">篇名信息</param>
        /// <returns></returns>
        public Boolean InsertZGCG_PM(ZGCG_PM zgcg_pm)
        {
            OperationLogItem operationLog = this.GetOperationLogItem(zgcg_pm, "INSERT", null);
            sqlMapper.Insert("InsertZGCG_PM", zgcg_pm);
            this.operationLogDAO.insertLogItem(operationLog);
            return true;
        }

        /// <summary>
        /// 更新一条分类信息
        /// </summary>
        /// <param name="zgcg_fl">要更新的分类信息</param>
        /// <returns></returns>
        public Boolean UpdateZGCG_FL(ZGCG_FL zgcg_fl)
        {
            Boolean flag = true;
            int back = sqlMapper.Update("UpdateZGCG_FL", zgcg_fl);
            if (back <= 0)
            {
                flag = false;
            }
            return flag;

        }
        /// <summary>
        /// 更新一条篇名信息
        /// </summary>
        /// <param name="zgcg_pm">要更新的篇名信息</param>
        /// <param name="uname">操作用户名</param>
        /// <returns></returns>
        public Boolean UpdateZGCG_PM(ZGCG_PM zgcg_pm, String uname)
        {
            Boolean flag = true;
            int back = sqlMapper.Update("UpdateZGCG_PM", zgcg_pm);
            if (back <= 0)
            {
                flag = false;
            }
            if (flag)
            {
                OperationLogItem operationLog = this.GetOperationLogItem(zgcg_pm, "UPDATE", uname);
                this.operationLogDAO.insertLogItem(operationLog);
            }
            return flag;
        }

        /// <summary>
        /// 按专业分类删除分类信息
        /// </summary>
        /// <param name="zyfl">专业分类</param>
        /// <returns></returns>
        public Boolean DeleteZGCG_FL(String zyfl)
        {
            Boolean flag = true;
            int back = sqlMapper.Delete("DeleteZGCG_FLByZyfl", zyfl);
            if (back <= 0)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 按ID编号删除
        /// </summary>
        /// <param name="ID">ID编号</param>
        /// <param name="uname">操作用户名</param>
        /// <returns></returns>
        public int DeleteZGCG_PM(object key, String uname)
        {
            int result = 0;
            String ID = key.ToString();
            int id = 0;

            try
            {
                id = Int32.Parse(ID);
            }
            catch(Exception)
            {
                id = 0;
            }

            ZGCG_PM zgcgPM = this.GetZGCG_PMByID(id);

            OperationLogItem operationLog = null;

            result = sqlMapper.Delete("DeleteZGCG_PMByID", id);
            operationLog = this.GetOperationLogItem(zgcgPM, "DELETE", uname);
            if (null != operationLog)
            {
                this.operationLogDAO.insertLogItem(operationLog);
            }

            return result;
        }

        /// <summary>
        /// 修改已有的成果编号
        /// </summary>
        /// <param name="oldCgbh">旧成果编号</param>
        /// <param name="newCgbh">新成果编号</param>
        /// <returns>是否修改成功</returns>
        public bool changeZGCGPMCgdm(String oldCgbh, String newCgbh)
        {
            if (null == oldCgbh || null == newCgbh)
            {
                return false;
            }

            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("oldcgbh", oldCgbh);
            para.Add("newcgbh", newCgbh);

            bool result = true;
            try
            {
                sqlMapper.Update("ChangeZGCG_PM_cgbh", para);
            }
            catch
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 根据成果编号,年份获取最大文件的篇名信息
        /// </summary>
        /// <param name="zyfl">专业分类</param>
        /// <param name="nf">年份</param>
        /// <returns></returns>
        public ZGCG_PM getMaxZGCG_PM(String zyfl, String nf)
        {
            String cgbh = null;

            if (null == zyfl || "".Equals(zyfl))
            {
            }
            else
            {
                cgbh = zyfl + "-";
                if (!"".Equals(nf) && nf.Length == 4)
                    cgbh += nf;
            }

            if (null != cgbh && !"".Equals(cgbh))
            {
                cgbh = cgbh + "%";
            }
            return sqlMapper.QueryForObject<ZGCG_PM>("SelectMaxZGCG_PM", cgbh);
        }

        /// <summary>
        /// 生成日志信息
        /// </summary>
        /// <param name="zgcgPM">中国成果信息</param>
        /// <param name="opType">操作类型</param>
        /// <param name="uname">用户名</param>
        /// <returns></returns>
        private OperationLogItem GetOperationLogItem(ZGCG_PM zgcgPM, String opType, String uname)
        {
            if (null == zgcgPM)
            {
                return null;
            }

            OperationLogItem operationLogItem = new OperationLogItem();
            //操作类型
            operationLogItem.opType = opType;
            //数据库表
            operationLogItem.opTable = "ZGCG_PM";

            operationLogItem.opObjectID = "中国成果";
            // 操作涉及的书刊
            operationLogItem.opBookIn = "中国成果";

            // 时间
            operationLogItem.opDate = DateTime.Now;
            // 操作员
            operationLogItem.opPerson = uname;
            //记录操作模式，判断是否属于批量操作。取值范围SINGLE/BATCH。
            operationLogItem.opMode = "SINGLE";

            //操作的内容
            String content = "('" + zgcgPM.flbm + "','" + 
                zgcgPM.cgbh + "','" + zgcgPM.cgmc + "','" + 
                zgcgPM.wcr + "','" + zgcgPM.wcdw + "','" + 
                zgcgPM.dwdz + "','" + zgcgPM.yzbm + "','" + 
                zgcgPM.dwdh + "','" + zgcgPM.cgjj + "','" + 
                zgcgPM.zwztc + "','" + zgcgPM.yjqzsj + "','" + 
                zgcgPM.gtfl + "','" + zgcgPM.cglb + "','" + 
                zgcgPM.cgsp + "','" + zgcgPM.jdbm + "','" +
                zgcgPM.jdrq + "','" + zgcgPM.lxhhj + "','" +
                zgcgPM.fbdw + "','" + zgcgPM.zlly + "','" +
                zgcgPM.fbnf + "','" + zgcgPM.lybh + "','" +
                zgcgPM.hydm + "','" + zgcgPM.hymc + "','" +
                zgcgPM.zlsq + "','" + zgcgPM.kf + "','" +
                zgcgPM.gxrq + "','" + 
                "')";
            operationLogItem.opContent = content;
            return operationLogItem;
        }

        public ZGCG_DAO()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
    }
}